package com.jessechanrui.template.core.dto;

import com.jessechanrui.template.core.constant.Constants;
import com.jessechanrui.template.core.exception.BaseErrorCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author chenrui
 * @version 1.0
 * @description 响应结果
 * @date 2025/8/8
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseResult<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    private boolean success;

    /**
     * 错误码
     */
    private String errCode;

    /**
     * 错误信息
     */
    private String errMessage;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 构建成功响应
     *
     * @return ResponseResult
     */
    public static ResponseResult<?> buildSuccess() {
        return ResponseResult.builder()
                .success(Constants.SUCCESS)
                .build();
    }

    /**
     * 构建失败响应
     *
     * @param errCode    错误码
     * @param errMessage 错误信息
     * @return ResponseResult
     */
    public static ResponseResult<?> buildFailure(String errCode, String errMessage) {
        return ResponseResult.builder()
                .success(Constants.FAILURE)
                .errCode(errCode)
                .errMessage(errMessage)
                .build();
    }

    /**
     * 构建失败响应
     *
     * @param errorCode 错误码
     * @return ResponseResult
     */
    public static ResponseResult<?> buildFailure(BaseErrorCode errorCode) {
        return buildFailure(errorCode.getCode(), errorCode.getMessage());
    }

    /**
     * 构建成功响应
     *
     * @param data 业务数据
     * @param <T>  业务数据类型泛型
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> of(T data) {
        ResponseResult<T> response = new ResponseResult<>();
        response.setSuccess(true);
        response.setData(data);
        return response;
    }

}
